import time

from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest,allure
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
@allure.feature("CRMEB测试")
class Test_CRMEB:
    @pytest.fixture(scope='function',autouse=True)
    def setup_model(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(15)
        self.driver.maximize_window()
        self.driver.get("http://60.205.190.66:8899/admin/login")
        inputs = self.driver.find_elements(By.TAG_NAME,"input")
        for i in inputs:
            if i.get_attribute("placeholder") =="请输入用户名":
                i.send_keys("admin")
            elif i.get_attribute("placeholder") == "请输入密码":
                i.send_keys("920509")
            elif i.get_attribute("placeholder") == "请输入验证码":
                i.send_keys("a")
        self.driver.find_element(By.CLASS_NAME,"btn").click()
        yield
        self.driver.quit()
    @allure.story("添加用户分组")
    def test_1(self):
        # 点击用户
        self.driver.find_elements(By.CLASS_NAME,"ivu-menu-submenu-title")[2].click()

        #点击用户分组 9
        self.driver.find_elements(By.CLASS_NAME,"ivu-menu-item")[9].click()

        # 点击添加用户分组 3
        self.driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/div[2]/div/div[2]/div/div[2]/div/div[1]/div/button/span").click()

        #输入分组名称
        WebDriverWait(self.driver,10).\
            until(expected_conditions.presence_of_element_located((By.CLASS_NAME,"ivu-input"))).send_keys("测试下小天才")

        # 点击添加
        self.driver.find_elements(By.CLASS_NAME,"ivu-btn")[4].click()

        #等待弹窗消失
        time.sleep(3)


        # 获取刚新增的名字，用.text返回文本信息
        # step 1 定位到父节点
        ivus = self.driver.find_elements(By.CLASS_NAME,"ivu-table-tbody")[0]

        # step 2 定通过父子定位，定位到最后一个tr
        tr = ivus.find_elements(By.TAG_NAME,"tr")[-1]

        # step 3 通过父子定位，定位到第二个td
        td = tr.find_elements(By.TAG_NAME,"td")[1]

        # step4 通过父亲td，定位子节点span
        text = td.find_element(By.TAG_NAME,"span").text
        print(text)
        # 使用assert 去做断言，判断是否和新增的一样
        try :
            assert text == "测试下小天才"
        except:
            self.driver.get_screenshot_as_file("./error.png")
            raise "断言失败！"


    @allure.story("删除用户分组")
    def test2(self):
        # 点击用户
        self.driver.find_elements(By.CLASS_NAME, "ivu-menu-submenu-title")[2].click()

        # 点击用户分组 9
        self.driver.find_elements(By.CLASS_NAME, "ivu-menu-item")[9].click()

        # 定位到想要的table，父节点
        tables = self.driver.find_elements(By.CLASS_NAME,"ivu-table-tbody")[1]

        # 定位到最后一组tr，子节点
        tr = tables.find_elements(By.TAG_NAME,"tr")[-1]

        #定位到tr里面的第一个td，子节点的子节点
        td = tr.find_elements(By.TAG_NAME,"td")[0]

        #收集所有的a标签，然后定位到删除按钮
        td.find_elements(By.TAG_NAME,"a")[1].click()

        # 点击弹窗中的确定  5
        button = WebDriverWait(self.driver, 10). \
            until(expected_conditions.presence_of_all_elements_located((By.CLASS_NAME, "ivu-btn")))[5]
        button.click()
